﻿using NSJL.DAL.DataModel.Entities;
using NSJL.DomainModel.Background.Client;
using NSJL.DomainModel.Background.System;
using NSJL.Framework.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace NSJL.Biz.Background.Client
{
    public class InterfaceBiz
    {
        public InterfaceBiz()
        {
            
        }
        public static string TokenStr { get; set; }
       

        public static AuthCommonResult GetAuth(string data)
        {
            try
            {
                var flag = 0;
                data = "{\"data\":\""+data+"\"}";
            ReStart:
                var result = GetRequest("http://121.40.134.164:8091/serialNumber/register",data);
                if (!result.result)
                {
                    if (flag < 1)
                    {
                        flag++;
                        goto ReStart;
                    }
                }
                var data1 = new JavaScriptSerializer().Deserialize<AuthCommonResult>(result.data);
                return data1;
            }
            catch (Exception e)
            {
                return null;
            }
        }


        public static CommonResult GetRequest(string url, string data = null, string method = "POST", bool Flag = true)
        {
            try
            {
                TextLogUtil.Info("请求url：" + url + "-----参数：" + (data ?? ""));

                ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => { return true; };
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
                ServicePointManager.DefaultConnectionLimit = 2000;

                string strBuff = "";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = "application/json";
                request.Method = method;
                request.ServicePoint.Expect100Continue = false;
                request.Timeout = 1000 * 10;
                request.ReadWriteTimeout = 1000 * 10;
                request.UserAgent = "PostmanRuntime/7.29.2";
                request.Headers.Add("Authorization", TokenStr);
                if (!string.IsNullOrWhiteSpace(data))
                {
                    byte[] postdata = System.Text.Encoding.UTF8.GetBytes(data);
                    request.ContentLength = postdata.Length;
                    Stream newStream = request.GetRequestStream();
                    newStream.Write(postdata, 0, postdata.Length);
                    newStream.Close();
                }

                HttpWebResponse httpResp = (HttpWebResponse)request.GetResponse();
                Stream respStream = httpResp.GetResponseStream();
                StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
                strBuff = respStreamReader.ReadToEnd();
                request.Abort();
                httpResp.Close();
                if (respStream != null)
                {
                    respStream.Close();
                }
                respStreamReader.Close();

                TextLogUtil.Info("返回：" + strBuff);

                return new CommonResult() { result = true, data = strBuff };
            }
            catch (Exception ex)
            {
                //TextLogUtil.Info(ex.Message);
                return new CommonResult() { result = false, message = ex.Message };
            }
        }
    }
}
